/**
 * @author Pawel Gadecki <pidzama@gmail.com>
 * 
 */
package spwodne;

import java.util.Iterator;
import java.util.List;
import javax.swing.table.AbstractTableModel;

public class MTWsi extends AbstractTableModel {

    MTWsi(List lista, int nr, float st) {
        l = lista;
        nrSpolki = nr;
        stawka = st;
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    public String getNaz(int nr) {
        return ((Wies) l.get(nr)).getNaz();
    }
    public Wies getWies(int nr){
        //na potrzeby wydruku klientow danej wsi
        return (Wies) l.get(nr);
    }

    @Override
    public int getRowCount() {
        return l.size();
    }

    @Override
    public String getColumnName(int col) {
        return columnNames[col];
    }

    @Override
    public Object getValueAt(int row, int col) {
        Object tmp;
        switch (col) {
            case 0: // '\0'

                tmp = ((Wies) l.get(row)).getID();
                break;

            case 1: // '\001'

                tmp = ((Wies) l.get(row)).getNaz();
                break;

            case 2: // '\002'

                tmp = ((Wies) l.get(row)).ile_ha;
                break;

            case 3: // '\003'

                tmp = ((Wies) l.get(row)).ile_zalega;
                break;

            default:
                System.out.println("getValue out of bounds");
                tmp = null;
                break;
        }
        return tmp;
    }

    @Override
    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    public void addRow(int nrWsi, String naz) {
        l.add(new Wies(Integer.valueOf(nrWsi), Integer.valueOf(nrSpolki), naz, Float.valueOf(stawka)));
        fireTableDataChanged();
    }

    public void editRow(int nrWsi, int nr, String naz) {
        ((Wies) l.get(nr)).setNaz(naz);
        ((Wies) l.get(nr)).id = Integer.valueOf(nrWsi);
        for (Iterator i$ = ((Wies) l.get(nr)).getLKli().iterator(); i$.hasNext();) {
            Klient k = (Klient) i$.next();
            k.nrWsi = Integer.valueOf(nrWsi);
        }

        fireTableDataChanged();
    }

    public void delRow(int n) {
        l.remove(n);
        fireTableDataChanged();
    }

    public void refresh() {
        Wies w;
        for (Iterator i$ = l.iterator(); i$.hasNext(); w.refresh()) {
            w = (Wies) i$.next();
        }
        fireTableDataChanged();
    }
    private List l;
    private int nrSpolki;
    private float stawka;
    private String columnNames[] = {
        "Id", "Nazwa", "Pow. [ha]", "Nale\u017Cno\u015B\u0107 [zl]"
    };
}
